package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.utils.JsonData;
import com.example.utils.config.DBTypeEnum;
import com.example.utils.config.MyDataSource;


@RestController
@RequestMapping("/user")
@MyDataSource(DBTypeEnum.DB2)
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;//查询操作不涉及事务所以也可以用 Mapper 层调用

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    public Object findAll(){
        List<User> all = userService.findAll();
        return JsonData.buildSuccess(all);
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping(value="/add", method=RequestMethod.POST)
    public Object add(User user){
        int id = userService.add(user);
        return JsonData.buildSuccess(id);
    }

    /**
     * 根据id查询
     * @return
     */
    @RequestMapping(value="/findById", method=RequestMethod.POST)
    public Object findById(Integer id){
        User user = userService.findById(id);
        return JsonData.buildSuccess(user);
    }
    
    
    /**
     * 根据name查询
     * @return
     */
    @RequestMapping(value="/findByName", method=RequestMethod.POST)
    public Object findById(String name){
        User user = userService.findByName(name);
        return JsonData.buildSuccess(user);
    }

    /**
     * 更新
     * @return
     */
    @RequestMapping("/update")
    public Object update(User user){
        userService.update(user);
        return JsonData.buildSuccess();
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    public Object delete(Integer id){
        userService.delete(id);
        return JsonData.buildSuccess();
    }
    
    
    
    
    
}

